<template>
	<section style="height:500px; overflow: auto;" v-loading="searchLogining">
		<el-tree
		  :data="menuTreeList"
		  show-checkbox
		  ref="tree"
		  node-key="id"
		  :default-checked-keys="isCheckedIds"
		  :props="defaultProps">
		</el-tree>
		<el-row class="saveButton">
			<el-button type="primary" @click="toAuth" :loading="buttonLoading">保存</el-button>
		</el-row>
	</section>
</template>
<script>
export default {
	data() {
	    return {
	    	//此处不能再次定义props的属性了
	    	menuTreeList:[],
	    	defaultProps:{
	    		id:'id',
	    		children:'children',
	    		label:'menu_name'
	    	},
	    	isCheckedIds:[],
	    	searchLogining:false,
	    	buttonLoading:false,
	    }
	},props:{
		//接受父级传的值属性
        role_id:0
    },methods:{
    	toAuth(){
    		var url = '/admin/role/toAuth';
            var token = this.Common.cookie('token');
            var data = {'token':token,'role_id':this.role_id,'menu_ids':this.$refs.tree.getCheckedKeys()}
            var tar = this;
            tar.buttonLoading = true;
			tar.Common.request(url, data, function(data){
	        	tar.buttonLoading = false;
	        	if(data.success_code==0){
	        		tar.$message({
						title: '警告',
						message: data.message,
						type: 'error'
					});
	        	}else{
	        		tar.$message({
						title: '成功',
						message: data.message,
						type: 'success'
					});
	        		//调用父类关闭当前窗口
					tar.$emit('closeAuthDialog')
	        	}
	        },function(data){
	        	var message = '接口请求失败';
	        	if(data.message){
	        		message = data.message;
	        	}
	        	tar.buttonLoading = false;
				tar.$message({
					title: '警告',
					message: message,
					type: 'error'
				});
	        },this);

    	},getMenuTreeList(){
			var url = '/admin/menu/getTreeLists';
            var token = this.Common.cookie('token');
            var data = {'token':token,'role_id':this.role_id,'status':1}
            var tar = this;
            tar.searchLogining = true;
			tar.Common.request(url, data, function(data){
	        	tar.searchLogining = false;
	        	if(data.success_code==0){
	        		tar.$message({
						title: '警告',
						message: data.message,
						type: 'error'
					});
	        	}else{
	        		tar.menuTreeList = data.result.lists;
	        		tar.isCheckedIds = data.result.isCheckedIds;
	        	}
	        },function(data){
	        	var message = '接口请求失败';
	        	if(data.message){
	        		message = data.message;
	        	}
	        	tar.searchLogining = false;
				tar.$message({
					title: '警告',
					message: message,
					type: 'error'
				});
	        },this);
		}
    },mounted(){
		this.getMenuTreeList();
	}
}
</script>
<style>
.saveButton{
	position: absolute;
	bottom: 30px;
	width: 70px;
	left: calc(50% - 35px);
}
</style>